/**
 * @转yy-mm-dd
 */

import React from 'react';
import moment from "moment";
import {ColumnsState} from "@ant-design/pro-table/lib/container";

export const reRedAndGreen = (text:string | number|null|undefined) => {
  if (!text) {
    return null
  }
  return Number(text) > 0 ? <div style={{color:'rgb(245,30,30)'}}>{text}</div> : Number(text) < 0 ? <div style={{color:'rgb(30,195,45)'}}>{text}</div>:<div>{text}</div>;
}

export const formatDate = (inputDate: any, isYYMMDD: boolean) => {
  if (!inputDate) {
    return null;
  }
  let date = new Date(inputDate);
  let YY = date.getFullYear() + '-';
  let MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  let DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
  let hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  let mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  let ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  return isYYMMDD ? YY + MM + DD : YY + MM + DD + " " + hh + mm + ss;
}

/**
 * @读取本地表格储存排序
 */
export const columnsSorter = (localString: string, columns: any[]) => {
  let columnsSorter = localStorage.getItem(localString);
  if (columnsSorter) {
    let jsonSorter: any = JSON.parse(columnsSorter);
    for (let a in jsonSorter) {
      columns.map(b => {
        if (b.dataIndex === a) {
          if (!b.order) {
            b.order = jsonSorter[a].order;
          }
          if (!b.fixed) {
            b.fixed = jsonSorter[a].fixed;
          }
          if (!b.show) {
            b.show = (jsonSorter[a].show !== false);
          }
        }

      })
      // debugger
      columns.sort((a, b) => {
        return a.order - b.order
      })
    }
    let ids = 0;
    // @ts-ignore
    for (let id in jsonSorter) {
      ids++
    }

    columns.map(a => {
      if (empty(a.order)) {
        a.order = ids
        ids++;
      }
      if (empty(a.show)) {
        a.order = true
      }
    })
  }
}


export const onColumnsStateChangeFunc = (e: Record<string, ColumnsState>, columns: any, str: string) => {
  if (!empty(e)) {
    for (let i = 0; i < columns.length; i++) {
      const column = columns[i];
      if (column.fixed) {
        e[column.dataIndex] = column.fixed;
      }
    }
    // @ts-ignore
    localStorage.setItem(str, JSON.stringify(e))
  }
}

export const columnToColumnsState = (columns: any[]) => {
  let col = {};
  if (!empty(columns)) {
    for (let i = 0; i < columns.length; i++) {
      const item = columns[i];
      let fixedItem = (item.fixed ? {fixed: item.fixed} : {})
      let orderItem = (item.order ? {order: item.order} : {})
      let showItem = (item.show === false ? {show: item.show} : {show: true})
      col[item.dataIndex] = {
        ...showItem,
        ...fixedItem,
        ...orderItem,
      };
    }

  }
  return col;
}

/**
 * @dNunPercent+%
 * @param num
 * @param fix
 */
export const formatNumPercent = (num: number | undefined | null, fix: number | undefined) => {
  let reNum = NumberMul(num, 100, fix);
  if (reNum !== null) {
    return reNum + "%"
  } else {
    return reNum
  }
}

export const formatNum = (num: number | undefined | null, fix: number | undefined) => {
    return NumberMul(num, 1, fix)
}

export function empty(v: any) {
  switch (typeof v) {
    case 'undefined':
      return true;
    case 'string':
      if (v.trim().length === 0) return true;
      if (v === 'undefined') return true;
      if (v === '') return true;
      if (v === 'NAN') return true;
      if (v === 'NaN') return true;
      break;
    case 'boolean':
      return false;
    case 'number':
      if (0 === v) {
        return false;
      }
      break;
    case 'object':
      if (null === v) return true;
      if (undefined !== v.length && v.length === 0) return true;
      // @ts-ignore
      for (let k in v) {
        return false;
      }
      return true;
  }
  return false;
}


export function NumberDiv(a: number | string | undefined | null, b: number | string | undefined | null, fix: number | undefined) {
  // number, 保留尾数
  if (empty(a) || empty(b) || b === 0) {
    return null;
  }
  if (!empty(fix)) {
    return Number((Number(a) / Number(b)).toFixed(fix));
  } else {
    return Number((Number(a) / Number(b)));
  }
}

export function NumberMul(a: number | undefined | string | null, b: number | undefined | null, fix: number | undefined) {
  // number, 保留尾数
  if (empty(a) || empty(b)) {
    return null;
  }
  if (!empty(fix)) {
    return Number((Number(a) * Number(b)).toFixed(fix));
  } else {
    return Number((Number(a) * Number(b)));
  }
}

/**
 *  @Describe:时间--HH-MM-dd
 *  Created by xjc on 2019/8/22
 */

export function getNowFormatDate(date: moment.Moment | null | undefined) {
  if (!date) {
    return null;
  }
  const seperator1 = '-';
  const year = date.year();
  let month: number | string = date.month() + 1;
  let strDate: number | string = date.date();
  if (month >= 1 && month <= 9) {
    month = `0${month}`;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = `0${strDate}`;
  }
  const currentdate = year + seperator1 + month + seperator1 + strDate;
  return currentdate;
}


/**
 *  @Describe:9999999.999 to '9,999,999. 999'
 *  @Created by xjc on 2019/6/6
 */

export function NumberDivide(str: any, fix: any, strAdd?: any) {
  // number, 保留尾数, 添加符号
  if (str === 'NaN' || str === 'null' || typeof str === 'undefined' || str === null || isNaN(str)) {
    return null;
  }
  if (strAdd === 'NaN' || typeof strAdd === 'undefined' || strAdd === null) {
    strAdd = '';
  }
  if (typeof str === 'number') {
    if (typeof fix === 'number') {
      str = str.toFixed(fix);
    } else {
      str = parseFloat(String(str)).toString();
    }
  }
  let a;
  let b;
  let c;
  let d;
  const re = /(?=(?!\b)(\d{3})+$)/g;
  a = str.split('.');
  b = a[0];
  c = b.replace(re, ',');
  d = typeof a[1] === 'string' ? `. ${a[1]}` : '';
  if (strAdd === '$') {
    return strAdd + c + d;
  }
  return c + d + strAdd;
}

/**
 *  Describe:切空格
 *  Created by xjc on 2019/7/4
 */

export function DateStamp(param:any) {
  // sDate1和sDate2是yyyy-MM-dd格式
  if (typeof param === 'string') {
    const str = param.replace(/-/g, '/');
    const date = new Date(str);
    return date.getTime();
  }
  return 0;
}
